
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <semaphore.h>

#include "hfile.h"
#define CMDSIZE 50
#define ARGSIZE BUFLEN - CMDSIZE


int commandAna(int descClient, char buffer[BUFLEN], char *dataBuffer)
{
    int iCount, iCountArg = 0;
    char cCommand[BUFLEN];
    char cArg[ARGSIZE];
    sem_t *semComChanPost;
    sem_t *semComChanRead;

    if((semComChanPost = sem_open("semComChanPost", 0, 0644, 1)) == SEM_FAILED)
    {
        perror("Open semComChanPost");
        if(sem_unlink("semcomChanPost") <0)
        {
            perror("semComChanPost");
        }

    }

    if((semComChanRead = sem_open("semComChanRead", 0)) == SEM_FAILED)
    {
        perror("Open semComChanRead");
        if(sem_unlink("semcommChanRead") <0)
        {
            perror("semCommChanRead");
        }

    }
    //Substract the command part and put the value in cCommand
    for(iCount = 0; buffer[iCount] != ' '; iCount++)
    {
        cCommand[iCount] = buffer[iCount];
    }
    cCommand[iCount] = '\0';

    iCount ++;  //Hop over space

    //Substract agrumand and put value in sArg, starting from the point that  icount left of.
    while((buffer[iCount]!= ' ') && (buffer[iCount]!= '\0'))
    {
        cArg[iCountArg] = buffer[iCount];
        ++iCount;
        iCountArg++;
    }

    iCountArg=0;    //Set iCountArg on zero so dat the next argument starts again with 0.

   
    //Use strcmp to compare cCommand value with any known commands
    if(strcmp(cCommand,"QUIT") == 0)
    {
        printf("Blaaaaaa\n");
        bzero(buffer,BUFLEN);
        bzero(cCommand,BUFLEN);
        close(descClient);
    }

     if(strcmp(cCommand,"UPLOAD") == 0)
    {
         printf("Het command is:%s\n", cCommand);
         printf("Argument 1 is:%s\n",cArg);


         //Stuur UPLOAD met parameters naar file proces
         //if(sem_wait(semComChanRead) < 0)
        // {
         //   perror("wait semComChanRead");
        // }
         askFsp(buffer);// --->Send the buffer with command, and arguments to mmap.
         if(sem_post(semComChanPost) < 0)
         {
             perror("post semCommChanPost");
         }

        if(sem_wait(semComChanRead) < 0)
        {
           perror("wait semCommRead");
        }
         //Luister naar FPS, als dat een ready signaal komt dan stuur data
         //listenFSP----->Als het goed is kan de functie al READY afvangen maar dit moet nog getest worden
          listenFsp(descClient,dataBuffer);
      //  if(sem_post(semComChanRead) < 0)
      //  {
      //      perror("post semCommRead");
      //  }
          bzero(buffer,BUFLEN);
          bzero(cCommand,BUFLEN);
    }

    if(strcmp(cCommand, "DOWNLOAD")==0)
    {

          //wait for sem to be unlocked, then lock
         //lock semaphore, decrease it to 0, set semaphore
        // if(sem_wait(semComChanRead) < 0)
        // {
        //    perror("wait semComChanRead");
       //  }
        //Stuur DOWNLOAD
        askFsp(buffer);// --->Send the buffer with command, and arguments to mmap.

       // if(sem_post(semComChanPost) < 0)
      //  {
      //      perror("post semCommChanPost");
      //  }

      //  if(sem_wait(semComChanPost) < 0)
     //   {
      //     perror("wait semCommPost");
      //  }
        //listenFSP----->Als het goed is kan de functie al READY afvangen maar dit moet nog getest worden
        listenFsp(descClient,dataBuffer);//If READYDOWNLOAD call recvFromFsp

      //  if(sem_post(semComChanRead) < 0)
     //   {
     //       perror("post semCommRead");
     //   }
        bzero(buffer,BUFLEN);
        bzero(cCommand,BUFLEN);
    }

    bzero(buffer,BUFLEN);
    bzero(cCommand,BUFLEN);
    return 0;
}


